import os

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common import utils

from page.main_page import MainPage


class App:
    driver: WebDriver = None

    @classmethod
    def start(cls):
        caps = {}
        caps["platformName"] = "Android"
        caps["appium:deviceName"] = "gxx"
        caps["appium:appPackage"] = "com.xueqiu.android"
        caps["appium:appActivity"] = ".view.WelcomeActivityAlias"
        caps["appium:autoGrantPermissions"] = "true"
        caps["udid"] = os.getenv("udid", None)
        caps["systemPort"] = utils.free_port()
        caps["chromedriverPort"] = utils.free_port()




        cls.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        cls.driver.find_element(AppiumBy.ID, "tv_agree").click()
        cls.driver.implicitly_wait(10)

        return MainPage(cls.driver)

    @classmethod
    def quit(cls):
        cls.driver.quit()
